package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.system.MarketStorage;
import com.barbeque.mall.bean.system.MarketStorageExample;
import com.barbeque.mall.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/17 15:52
 */
@Service
@Transactional
public class SysStorageServiceImpl implements SysStorageService {
   
   @Autowired
    MarketStorageMapper marketStorageMapper;
   
   /**
    * 创建对象存储
 * @param storage 
    * @return int
    * @author 苏东宇
    * @since 2022/07/17 16:18 
    */
    @Override
    public int CreateStorage(MarketStorage storage) {
        int i = marketStorageMapper.insertSelective(storage);
        return i;
    }

    /**
     * 获取对象列表，当key和name不为空时，添加key和name的条件进行查询
     * key为精准查询，name为模糊查询
 * @param basePageInfo
 * @param key
 * @param name
     * @return com.barbeque.mall.bean.param.CommonData
     * @author 苏东宇
     * @since 2022/07/18 20:27
     */
    @Override
    public CommonData getStorageList(BasePageInfo basePageInfo, String key, String name) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(key)){
            criteria.andKeyEqualTo(key);
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        PageInfo<MarketStorage> marketStoragePageInfo = new PageInfo<>(marketStorages);

        return CommonData.data(marketStoragePageInfo);
    }

    /**
     * 删除对象
 * @param marketStorage
     * @return int
     * @author 苏东宇
     * @since 2022/07/18 21:12
     */
    @Override
    public int deleteStorage(MarketStorage marketStorage) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        criteria.andIdEqualTo(marketStorage.getId());

        int i = marketStorageMapper.updateByExample(marketStorage,marketStorageExample);

        return i;
    }
}
